//
//  GHUtils.m
//  GodHorses
//
//  Created by Mac on 2017/11/14.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "TMUtils.h"

@implementation TMUtils
+ (void)netWorkMonitorinCanUseWithHandle:(void (^)())handle{
    
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
            if (handle) {
                handle();
            }
        }
    }];
    [manager startMonitoring];
}
+ (UIView*)headerViewWithTitle:(NSString*)title{
    UIView * sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 44)];
    sectionHeader.backgroundColor = [UIColor whiteColor];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(15, 0, UIScreenWidth - 30, 44)
                                                     text:title
                                                textColor:TM_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    
    [sectionHeader addSubview:titleLable];
    return sectionHeader;
}
+ (UINavigationBar *)navigationBar{
    UINavigationController* nav = (id)[UIApplication sharedApplication].keyWindow.rootViewController.childViewControllers.firstObject;
    return nav.navigationBar;
}
+  (UITabBar *)tabbar{
    UITabBarController* tab = (id)[UIApplication sharedApplication].keyWindow.rootViewController;
    return tab.tabBar;
}
+ (CGFloat)navgationBarBootom{
    if (KIsiPhoneX) {
        return 88;
    }
    return 64;
}
+ (CGFloat)tabBarTop{
    if (KIsiPhoneX) {
        return (UIScreenHeight - 83);
    }
    return (UIScreenHeight - 49);
}
+ (void)debugSimulationNetWorkWithHadle:(void (^)(NSInteger, NSMutableArray *))handle{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSInteger num = 1;
    NSInteger randrom = arc4random()%100;
    NSMutableArray * array = [NSMutableArray new];
    for (NSInteger i = 0; i < randrom; i ++) {
        [array addObject:@(i)];
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(num * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
        if (handle) {
            handle(num,array);
        }
    });
}
+ (void)sendNoJsonRequestWithUrl:(NSString *)url params:(NSDictionary *)params isPost:(BOOL)isPost handle:(void (^)(BOOL, NSString *))handle{
    AFHTTPSessionManager * manger = [AFHTTPSessionManager manager];
    manger.requestSerializer = [AFHTTPRequestSerializer serializer];
    manger.responseSerializer = [AFHTTPResponseSerializer serializer];
    manger.requestSerializer.timeoutInterval = 20;
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    if (isPost) {
        [manger POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSString * string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            if (handle) {
                handle(YES,string);
            }
            [SVProgressHUD dismiss];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (handle) {
                handle(NO,nil);
            }
            [SVProgressHUD dismiss];
        }];
    }else{
        [manger GET:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSString * string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            if (handle) {
                handle(YES,string);
            }
            [SVProgressHUD dismiss];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (handle) {
                handle(NO,nil);
            }
            [SVProgressHUD dismiss];
        }];
    }
    
    
}
@end
